using System;
using System.Collections.Generic;
using System.Text;

namespace GoogleCalendarReminder
{
    /// <summary>
    /// A class which provides methods for retrieving Google.GData.Calendar.EventEntry objects from
    /// a Google Calendar service.
    /// </summary>
    public class EventProvider
    {
        #region Private Data Members

        private static readonly string _feedUrl = Properties.Settings.Default.FeedUrl;

        #endregion

        #region Public Functions

        /// <summary>
        /// Retrieves all calendar events.
        /// </summary>
        /// <param name="service">The calendar service to retrieve the events from.</param>
        /// <returns>An array of all events.</returns>
        /// <exception cref="Google.GData.Client.LoggedException">An error occured while communicating with the Google Calendar service.</exception>
        public static Google.GData.Calendar.EventEntry[] GetEvents(
            Google.GData.Calendar.CalendarService service)
        {
            Google.GData.Calendar.EventQuery query =
                new Google.GData.Calendar.EventQuery(_feedUrl);

            return GetEvents(service, query.StartTime, query.EndTime);
        }

        /// <summary>
        /// Retrieves all calendar events between specified dates.
        /// </summary>
        /// <param name="service">The calendar service to retrieve the events from.</param>
        /// <param name="startTime">The start time of the events to retrieve.</param>
        /// <param name="endTime">The end time of the events to retrieve.</param>
        /// <returns>An array of all events between the specified dates.</returns>
        /// <exception cref="Google.GData.Client.LoggedException">An error occured while communicating with the Google Calendar service.</exception>
        public static Google.GData.Calendar.EventEntry[] GetEvents(
            Google.GData.Calendar.CalendarService service,
            DateTime startTime,
            DateTime endTime)
        {
            Google.GData.Calendar.EventQuery query =
                new Google.GData.Calendar.EventQuery(_feedUrl);

            query.StartTime = startTime;
            query.EndTime = endTime;
            
            Google.GData.Calendar.EventFeed feed = service.Query(query);

            List<Google.GData.Calendar.EventEntry> entries =
                new List<Google.GData.Calendar.EventEntry>(feed.Entries.Count);

            foreach (Google.GData.Calendar.EventEntry entry in feed.Entries)
            {
                entries.Add(entry);
            }

            return entries.ToArray();
        }

        #endregion
    }
}
